package cn.powerone.scm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.powerone.common.utils.poi.ExcelExp;
import cn.powerone.common.utils.poi.ExcelUtilManySheet;
import cn.powerone.scm.domain.*;
import cn.powerone.scm.service.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmSupplier;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商Controller
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/scm/supplier")
public class ScmSupplierController extends BaseController
{
    @Autowired
    private IScmSupplierService scmSupplierService;
    @Autowired
    private IScmSupplierContactsService scmSupplierContactsService;
    @Autowired
    private IScmSupplierAddressService scmSupplierAddressService;

    /**
     * 查询供应商列表
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmSupplier scmSupplier)
    {
        startPage();
        List<ScmSupplier> list = scmSupplierService.selectScmSupplierList(scmSupplier);
        return getDataTable(list);
    }

    /**
     * 获取供应商详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmSupplierService.selectScmSupplierById(id));
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:add')")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmSupplier scmSupplier)
    {
        return toAjax(scmSupplierService.insertScmSupplier(scmSupplier));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:edit')")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmSupplier scmSupplier)
    {
        return toAjax(scmSupplierService.updateScmSupplier(scmSupplier));
    }

    /**
     * 删除供应商
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:remove')")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmSupplierService.deleteScmSupplierByIds(ids));
    }

    /**
     * 导出供应商列表
     */
    @PreAuthorize("@ss.hasPermi('scm:supplier:export')")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmSupplier scmSupplier) throws IOException {
        List<ScmSupplier> list = scmSupplierService.selectScmSupplierList(scmSupplier);
//        ExcelUtil<ScmSupplier> util = new ExcelUtil<ScmSupplier>(ScmSupplier.class);
//        util.exportExcel(response, list, "供应商数据");
        List<ScmSupplierContacts> contactsList = scmSupplierContactsService.selectScmSupplierContactsList(new ScmSupplierContacts());
        for (ScmSupplierContacts contacts : contactsList){
            contacts.setSupplierNo(scmSupplierService.selectScmSupplierById(contacts.getSupplierId()).getSupplierNo());
        }
        List<ScmSupplierAddress> addressList = scmSupplierAddressService.selectScmSupplierAddressList(new ScmSupplierAddress());
        for (ScmSupplierAddress address : addressList){
            address.setSupplierNo(scmSupplierService.selectScmSupplierById(address.getSupplierId()).getSupplierNo());
        }

        ExcelExp firstSheet = new ExcelExp("供应商信息", list, ScmSupplier.class);
        ExcelExp secondSheet = new ExcelExp("联系人", contactsList, ScmSupplierContacts.class);
        ExcelExp thirdSheet = new ExcelExp("地址", addressList, ScmSupplierAddress.class);
        List<ExcelExp> mySheet = new ArrayList<>();
        mySheet.add(firstSheet);
        mySheet.add(secondSheet);
        mySheet.add(thirdSheet);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mySheet);
        util.exportExcelManySheet(response, mySheet);
    }

    /**
     * 导入模板下载
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:import')")
    @Log(title = "供应商信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
//        ExcelUtil<ScmSupplier> util = new ExcelUtil<ScmSupplier>(ScmSupplier.class);
//        util.importTemplateExcel(response, "供应商信息");
        //new way
        ExcelExp firstSheet = new ExcelExp("供应商信息", null, ScmSupplier.class);
        ExcelExp secondSheet = new ExcelExp("联系人", null, ScmSupplierContacts.class);
        ExcelExp thirdSheet = new ExcelExp("地址", null, ScmSupplierAddress.class);
        List<ExcelExp> mySheet = new ArrayList<>();
        mySheet.add(firstSheet);
        mySheet.add(secondSheet);
        mySheet.add(thirdSheet);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mySheet);
        util.exportExcelManySheet(response, mySheet);

    }
    /**
     * 导入数据
     */
    @PreAuthorize("@ss.hasPermi('scm:customer:import')")
    @Log(title = "客户", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
//        ExcelUtil<ScmSupplier> util = new ExcelUtil<ScmSupplier>(ScmSupplier.class);
//        List<ScmSupplier> scmSupplier = util.importExcel(file.getInputStream());
//        String operName = getUsername();
//        String message = scmSupplierService.importData(scmSupplier, updateSupport, operName);
//        return success(message);

        // 导入多个sheet的数据
        Map<String, List<?>> importDataMap = ExcelUtilManySheet.importMultipleSheets(file,
                Arrays.asList("供应商信息", "联系人","地址"),
                new Class[]{ScmSupplier.class, ScmSupplierContacts.class,ScmSupplierAddress.class});

        // 处理导入的数据
        List<ScmSupplier> scmSupplierList = (List<ScmSupplier>) importDataMap.get("供应商信息");
        List<ScmSupplierContacts> contactsList = (List<ScmSupplierContacts>) importDataMap.get("联系人");
        List<ScmSupplierAddress> addressList = (List<ScmSupplierAddress>) importDataMap.get("地址");

        String operName = getUsername();
        String message = scmSupplierService.importData(scmSupplierList, updateSupport, operName);
        scmSupplierContactsService.importData(contactsList, updateSupport, operName);
        scmSupplierAddressService.importData(addressList, updateSupport, operName);

        return AjaxResult.success("导入成功");
    }
}
